package com.goal98.team.domain

import com.goal98.grails.plugin.account.domain.Debit

class Activity {

    static searchable = {
        analyzer 'chinese'
        venue component: true
        team component: true
//        personAttend component: true
    }

    String description
    Date activityTime
    BigDecimal actualPersonCount = 0
    Team team
    Venue venue
    Boolean doneDebit = false

    static hasMany = [personAttend:Person, costs:Debit]
    static belongsTo = Person

    static transients = ['totalCost', 'costOfEachPerson']

    BigDecimal getTotalCost(){
        BigDecimal result = 0
        costs?.each{Debit cost->
            result += cost.amount
        }
        return result
    }

    BigDecimal getCostOfEachPerson(){
        BigDecimal result = 0
        if (actualPersonCount != 0)
            result = this.totalCost.divide(actualPersonCount, 2, BigDecimal.ROUND_HALF_UP)
        return result 
    }

    static constraints = {
        description(blank:true)
        venue(nullable:true)
    }

    String toString(){
        return "$id -- [$team] [$venue] [$activityTime]"
    }

    static mapping = {
        cache true
        personAttend cache:true, include:'non-lazy'
        costs cache:true, include:'non-lazy'
        team cache:true
        venue cache:true
    }
}
